package org.geektimes.interceptor;

import org.eclipse.microprofile.faulttolerance.Bulkhead;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

public class InterceptorJdkProxy {

    public  Object create(Class clazz) {
        ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
        Object proxy = null;
        if(clazz.getAnnotation(Bulkhead.class)!=null){
            proxy = Proxy.newProxyInstance(classLoader, new Class[]{clazz}, new InvocationHandler() {
                @Override
                public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
                    System.out.println("hello proxy");
                    return method.invoke(null,args);
                }
            });
        }
        return proxy;
    }

    public  Object create(Class clazz,Object target) {
        ClassLoader classLoader = Thread.currentThread().getContextClassLoader();

        Object proxy = null;
        if(clazz.getAnnotation(Bulkhead.class)!=null){
            proxy = Proxy.newProxyInstance(classLoader, new Class[]{clazz}, new InvocationHandler() {
                @Override
                public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
                    System.out.println("hello proxy");
                    return method.invoke(target,args);
                }
            });
        }
        return proxy;
    }
}
